#ifndef SERVER_H
#define SERVER_H

#include <pthread.h>
#include <stdbool.h>
#include <limits.h>  // 添加这行来包含 PATH_MAX 定义
#include <mysql/mysql.h> // ???

#define PATH_MAX 4096  // 如果系统没有定义 PATH_MAX，我们自己定义一个合理的值

#define SERVER_PORT 8081
#define MAX_LISTENS 10
#define MAX_USERNAME_LEN 32
#define MAX_PASSWORD_LEN 32
#define MAX_CLIENTS 100
#define FTP_ROOT_DIR "."

typedef struct {
    char username[MAX_USERNAME_LEN];
    char password[MAX_PASSWORD_LEN];
    bool is_logged_in;
    int socket;
    char root_dir[PATH_MAX];  // 用户的根目录路径
    char current_dir[PATH_MAX];  // 新增字段，存储当前会话中客户所在的目录
} ClientInfo;

typedef struct {
    int server_socket;
    ClientInfo clients[MAX_CLIENTS];
    int client_count;
    pthread_mutex_t clients_mutex;
} ServerInfo;

// 定义消息结构
typedef struct {
    int type;
    int length;
    char data[1024];
} Message;

// 原有函数声明
int create_server_socket();
void bind_and_listen(int server_sock);
int accept_client(int server_sock);
void handle_client(ServerInfo* server, int client_index);
int handle_register(ServerInfo* server, int client_index, const char* username, const char* password);
int handle_login(ServerInfo* server, int client_index, const char* username, const char* password);
void init_server(ServerInfo* server);

// 新增函数声明
int handle_cd(ServerInfo* server, int client_index, const char* path);
int handle_ls(ServerInfo* server, int client_index, const char* path);
int handle_pwd(ServerInfo* server, int client_index);
int handle_mkdir(ServerInfo* server, int client_index, const char* dirname);
int handle_rm(ServerInfo* server, int client_index, const char* filename);
int handle_puts(ServerInfo* server, int client_index, const char* filename);
int handle_gets(ServerInfo* server, int client_index, const char* filename);

// 新增函数声明
void ensure_user_directory(const char* username);
char* get_full_path(const char* root_dir, const char* current_dir, const char* path);

//////////////////////////////////////////////////////////////////////// 自定义协议部分

// 定义消息类型
#define MSG_TYPE_LOGIN 1
#define MSG_TYPE_LOGOUT 2
#define MSG_TYPE_DATA 3
#define MSG_TYPE_REGISTER 4
#define MSG_TYPE_REGISTER_SUCCESS 5
#define MSG_TYPE_REGISTER_FAIL 6
#define MSG_TYPE_LOGIN_SUCCESS 7
#define MSG_TYPE_LOGIN_FAIL 8

// 新增文件操作相关的消息类型
#define MSG_TYPE_CD 9
#define MSG_TYPE_LS 10
#define MSG_TYPE_PUTS 11
#define MSG_TYPE_GETS 12
#define MSG_TYPE_RM 13
#define MSG_TYPE_PWD 14
#define MSG_TYPE_MKDIR 15

// 操作结果消息类型
#define MSG_TYPE_SUCCESS 16
#define MSG_TYPE_FAIL 17



// 声明协议函数
int send_message(int socket, Message *msg);
int receive_message(int socket, Message *msg);

#endif
